package com.gip.service.Impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gip.mapper.ProductMapper;
import com.gip.pojo.Product;
import com.gip.service.ProductService;

import javax.annotation.Resource;


@Service
public class ProductServiceImpl implements ProductService {

    @Resource
    ProductMapper productMapper;

    @Override
    public List<Product> findAllProduct() {
        return productMapper.findAllProduct();
    }

    @Override
    public List<Product> findAllProductByIf(Product product) {
        return productMapper.findAllProductByIf(product);
    }

    @Override
    public int addProduct(Product product) {
        Long existType = this.isExistType(product.getDictItemName());
        int roud = new Random().nextInt(10000) + 1;
        if (existType > 1) {//存在
            product.setProductType(existType.intValue());
        } else {
            this.addType(Long.valueOf(roud), "商品类别", product.getDictItemName());
            product.setProductType(roud);
        }
        int i = productMapper.addProduct(product);
        return i;
    }

    //判断类型是否存在 :存在返回类型码，不存在返回空
    @Override
    public Long isExistType(String type) {
        Map o = productMapper.isExistType(type);
        if (null == o || o.isEmpty()) {
            return 0L;
        }
        Long o1 = (Long) o.get("dict_type_code");
        return o1;
    }


    //新增类型 字典
    @Override
    public int addType(Long dictTypeCode, String dictTypeName, String dictItemName) {
        int i = productMapper.insertDicType(dictTypeCode, dictTypeName, dictItemName);
        return i;
    }

    @Override
    public int deleteProduct(int id) {
        return productMapper.deleteProduct(id);
    }

    @Override
    public int updateProduct(Product product) {
        return productMapper.updateProduct(product);
    }
}
